package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;

/**
 * @Author LEO
 * @Date 2021年12月10日 10:55
 */
@Api(tags = "商品Spu信息接口")
@RestController
@RequestMapping("admin/product")
public class SpuManageController {
    @Autowired
    private ManageService manageService;


    // http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
    //  select * from spuInfo where category where category3Id =? limit page ,limit
    // springmvc有个知识点 对象方式传值  spuInfo.getCategory3Id()
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo){
        // 分页查询
        Page<SpuInfo> spuInfoPage=new Page<>(page,limit);
      IPage<SpuInfo> iPage= manageService.getSpuInfoList(spuInfoPage,spuInfo);
      return Result.ok(iPage);
    }


    // 销售属性http://api.gmall.com/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        // 查询所有的销售属性集合
        List<BaseSaleAttr>  baseSaleAttrList = manageService.getBaseSaleAttrList();

        return Result.ok(baseSaleAttrList);
    }

    //http://api.gmall.com/admin/product/saveSpuInfo
    //保存Spu
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
            manageService.saveSpuInfo(spuInfo);
            return Result.ok();
    }

    //select * from spu_image where spu_id = spuId
    //	http://api.gmall.com/admin/product/spuImageList/29
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
       List<SpuImage> spuImagesList= manageService.getSpuImageList(spuId);
       return Result.ok(spuImagesList);
    }


    //回显销售属性+销售属性值！
    //	http://api.gmall.com/admin/product/spuSaleAttrList/29
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId){
      List<SpuSaleAttr> spuSaleAttrList=  manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }




}
